//
//  PKCommunityGroupTableViewCell.m
//  PianKe
//
//  Created by ybq on 15/12/30.
//  Copyright © 2015年 B7_Young. All rights reserved.
//

#import "PKCommunityGroupTableViewCell.h"
#import "PKCommunityButtonModel.h"

@implementation PKCommunityGroupTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    _headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 40, 40)];
    _headerImageView.layer.cornerRadius = 20;
    _headerImageView.layer.masksToBounds = YES;
    [self.contentView addSubview:_headerImageView];
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 20, 180, 20)];
    _titleLabel.font = [UIFont systemFontOfSize:20];
    [self.contentView addSubview:_titleLabel];
    
    UIImageView *tagImageView = [[UIImageView alloc] initWithFrame:CGRectMake(70, 50, 10, 10)];
    tagImageView.image = [UIImage imageNamed:@"biaoqian"];
    [self.contentView addSubview:tagImageView];
    
    _tagLabel = [[UILabel alloc] initWithFrame:CGRectMake(85, 50, 150, 10)];
    _tagLabel.font = [UIFont systemFontOfSize:10];
    _tagLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:_tagLabel];
    
    _memberLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth - 90, 50, 70, 10)];
    _memberLabel.font = [UIFont systemFontOfSize:10];
    _memberLabel.textColor = [UIColor grayColor];
    _memberLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_memberLabel];
    
    for (int i = 0; i < 4; i++) {
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.frame = CGRectMake(20 + i * 85, 80, 80, 80);
        button.tag = i + 1000;
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:12];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.tag = i + 100;
        [button addSubview:titleLabel];
        
        UILabel *contentLabel = [[UILabel alloc] init];
        contentLabel.font = [UIFont systemFontOfSize:10];
        contentLabel.numberOfLines = 0;
        contentLabel.textColor = [UIColor lightGrayColor];
        contentLabel.tag = i + 200;
        [button addSubview:contentLabel];

        
        [self.contentView addSubview:button];
    }
    
    _unameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 180, 100, 10)];
    _unameLabel.textColor = [UIColor lightGrayColor];
    _unameLabel.font = [UIFont systemFontOfSize:9];
    [self.contentView addSubview:_unameLabel];
    
    _lastUpdateLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth - 150, 180, 130, 10)];
    _lastUpdateLabel.textColor = [UIColor lightGrayColor];
    _lastUpdateLabel.font = [UIFont systemFontOfSize:9];
    _lastUpdateLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_lastUpdateLabel];
}

- (void)setModel:(PKCommunityGroupModel *)model {
    _model = model;
    [_headerImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg] placeholderImage:nil];
    _titleLabel.text = model.title;
    _tagLabel.text = [model.tags componentsJoinedByString:@"/"];
    _memberLabel.text = [NSString stringWithFormat:@"%@人加入",model.membernum];
    
    NSArray *latestposts = model.latestposts;
    for (int i = 0; i < 4; i++) {
        UIButton *button = [self.contentView viewWithTag:i + 1000];
        PKCommunityButtonModel *buttonModel = [[PKCommunityButtonModel alloc] init];
        [buttonModel setValuesForKeysWithDictionary:latestposts[i]];
        UILabel *titleLabel = [button viewWithTag:i + 100];
        UILabel *contentLabel = [button viewWithTag:i + 200];
        if ([buttonModel.coverimg isEqualToString:@""] && [buttonModel.songid isEqualToString:@""]) {
            button.backgroundColor = [UIColor grayColor];
            [titleLabel setFrame:CGRectMake(5, 5, 70, 10)];
            titleLabel.text = buttonModel.title;
            
            [contentLabel setFrame:CGRectMake(5, 44, 70, 30)];
            contentLabel.text = buttonModel.content;
            
            [button setImage:nil forState:(UIControlStateNormal)];
        } else if (![buttonModel.songid isEqualToString:@""]) {
            titleLabel.text = nil;
            contentLabel.text = nil;
            [button setImage:[UIImage imageNamed:@"music"] forState:(UIControlStateNormal)];
        } else {
            
            [button sd_setImageWithURL:[NSURL URLWithString:buttonModel.coverimg] forState:(UIControlStateNormal) placeholderImage:nil];
        }
       
    }
    
    _unameLabel.text = [NSString stringWithFormat:@"组长: %@",model.uname];
    _lastUpdateLabel.text = [NSString stringWithFormat:@"最后回复 - %@",model.lastupdate_f];
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
